<?php

namespace app\teacher\controller\v1;

use app\common\controller\BaseController;
use think\Controller;
use think\Request;
use app\teacher\model\Language as LanguageModel;
use app\teacher\model\Infomation as InfomationModel;
use app\teacher\model\InfomationCategory as InfomationCategoryModel;

class Infomation extends BaseController
{
    public function cate()
    {
        $infoCate = InfomationCategoryModel::getAllOnline();
        $this->success('', compact('infoCate'));
    }

    /**
     * 首页资讯
     */
    public function recommend()
    {
        $infoCateId = $this->notEmpty('info_cate_id');
        $infomations = InfomationModel::getRecommendByLanguageId($infoCateId);
        $this->success('', compact('infomations'));
    }

    /**
     * 资讯列表
     */
    public function index()
    {
        $infoCateId = $this->notEmpty('info_cate_id');
        $size = $this->request->get('size');
        $infomations = InfomationModel::getByLanguageId($infoCateId, $size);
        $infomations->total() ? $this->success('', [
            'infomations' => $infomations->items(),
            'total' => $infomations->total()
        ]) : $this->notFoundData();
    }

    /**
     * 资讯详情
     */
    public function read()
    {
        $infomationId = $this->notEmpty('infomation_id');
        $infomation = InfomationModel::find($infomationId);
        if (!$infomation) {
            $this->errorMsg(19000);
        }
        //其他推荐
        $other = InfomationModel::getOther($infomationId, $infomation->info_cate_id);
        //记录浏览量
        $res = $infomation->setInc('reading');
        if (!$res) {
            $this->forbidden();
        }
        $this->success('', ['infomation' => $infomation, 'other' => $other]);
    }

    /**
     * 搜索资讯
     */
    public function search()
    {
        $searchName = $this->request->get('search_name');
        $size = $this->request->get('size');
        $infomations = InfomationModel::search($searchName, $size);
        $infomations->total() ? $this->success('', [
            'infomations' => $infomations->items(),
            'total' => $infomations->total()
        ]) : $this->notFoundData();
    }

}
